package vcp.gui.forms;

import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import vcp.common.IView;
import vcp.gui.components.VCPPanel;

import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import client.Message;




import java.awt.Color;
import java.util.Observable;
import java.util.Observer;
import javax.swing.border.TitledBorder;

/**
 * Cancel order form
 */
public class CancelOrderForm extends VCPPanel implements Observer,IView {

	
	private static final long serialVersionUID = 1L;
	private JTextField textOrderID;
	private JButton btnCancelOrder;
	private JLabel lblCancellOrderError;
	private JTextField textUserId;

	public JTextField getTextUserId() {
		return textUserId;
	}




	public void setTextUserId(JTextField textUserId) {
		this.textUserId = textUserId;
	}




	/**
	 * Create the panel.
	 */
	public CancelOrderForm() {
		setBorder(new TitledBorder(null, "Order Cancellation", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(Color.WHITE);
		
		lblCancellOrderError = new JLabel("");
		lblCancellOrderError.setForeground(Color.RED);
		JLabel lblOrderID = new JLabel("Order ID");
		textOrderID = new JTextField();
		textOrderID.setColumns(10);

		btnCancelOrder = new JButton("Cancel Order");
		
		textUserId = new JTextField();
		textUserId.setColumns(10);
		
		JLabel labelUid = new JLabel("User ID");
		

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblCancellOrderError)
								.addComponent(labelUid, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblOrderID))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(textOrderID, GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
								.addComponent(textUserId, GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)))
						.addComponent(btnCancelOrder, Alignment.TRAILING))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(12)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCancellOrderError)
							.addGap(12))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(labelUid)
								.addComponent(textUserId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOrderID)
						.addComponent(textOrderID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(btnCancelOrder)
					.addContainerGap(194, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
	}
	
	
	

	@Override
	public void addController(Object obj) {
		btnCancelOrder.addActionListener((ActionListener)obj);
		btnCancelOrder.setActionCommand("cancelOrder");
	}

	@Override
	public void update(Observable o, Object arg) {
		Message tempMsg = (Message)arg;  
		switch(tempMsg.getMsg())
		 {
		 	case "CancelOrder":
				if(tempMsg.isStatus())
				{
					JOptionPane.showMessageDialog(null , "Cancel Order is Succesful");
				}	
				else
				{
					JOptionPane.showMessageDialog(null,"Cancel Order has Failed: " + tempMsg.getStatusMsg(),"DATABASE ERROR",JOptionPane.ERROR_MESSAGE);
				}
				break;
		 }
	}




	public JTextField getTextOrderID() {
		return textOrderID;
	}




	public void setTextOrderID(JTextField textOrderID) {
		this.textOrderID = textOrderID;
	}




	public JButton getBtnCancelOrder() {
		return btnCancelOrder;
	}




	public void setBtnCancelOrder(JButton btnCancelOrder) {
		this.btnCancelOrder = btnCancelOrder;
	}




	public JLabel getLblCancellOrderError() {
		return lblCancellOrderError;
	}




	public void setLblCancellOrderError(JLabel lblCancellOrderError) {
		this.lblCancellOrderError = lblCancellOrderError;
	}
}